package com.jy.adscm.operation.lxcg.controller;


import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.service.CgBomSupplierService;
import com.jy.adscm.operation.lxcg.bean.LxcgBomSupplier;
import com.jy.adscm.operation.lxcg.service.LxcgBomSupplierService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * bom与供应商 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@RestController
@RequestMapping("/adscm/operation/lxcg/lxcgBomSupplier")
public class LxcgBomSupplierController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    LxcgBomSupplierService lxcgBomSupplierService;

    /**
     * 增加bom信息
     * @param lxcgBomSupplier
     * @param request
     * @return
     */
    @AdscmLogger("增加bom与供应商关系信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody LxcgBomSupplier lxcgBomSupplier, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        lxcgBomSupplier.setCreator(token.getUser().getId().toString());
        lxcgBomSupplier.setCreateTime(new Date());
        boolean insert = lxcgBomSupplierService.insert(lxcgBomSupplier);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    /**
     * 修改bom信息
     * @param lxcgBomSupplier
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @AdscmLogger("修改bom与供应商关系信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody LxcgBomSupplier lxcgBomSupplier, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        lxcgBomSupplier.setCreator(token.getUser().getId().toString());
        lxcgBomSupplier.setCreateTime(new Date());
        boolean result = lxcgBomSupplierService.updateById(lxcgBomSupplier);
        return  new SimpleCoreResponse("200","修改成功",result);
    }


    /**
     * 根据图纸id查询bom信息
     * @param bid
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectByBid/{bid}")
    public SimpleCoreResponse selectByBid(@PathVariable Long bid , HttpServletRequest request) throws AdscmSystemException {
        List<LxcgBomSupplier> cgBomSuppliers = lxcgBomSupplierService.selectByBid(bid);
        return  new SimpleCoreResponse("200","查询成功",cgBomSuppliers);
    }
}

